package com.fgw.service.impl;

import com.fgw.common.ServerResponse;
import com.fgw.dao.PxtonzhiMapper;
import com.fgw.pojo.Pxtonzhi;
import com.fgw.pojo.Teacher;
import com.fgw.service.IPxtonzhiService;
import com.fgw.util.DateTimeUtil;
import com.fgw.vo.PxtonzhiListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by lyj on 2017/9/8.
 */
@Service("iPxtonzhiService")
public class PxtonzhiServiceImpl implements IPxtonzhiService {

    @Autowired
    private PxtonzhiMapper pxtonzhiMapper;

    /**
     * 显示培训列表
     * @return
     */
    public ServerResponse<PxtonzhiListVo> getNeedListTongzhi(){

        List<Pxtonzhi> Pxlist=pxtonzhiMapper.getNeedListTongzhi();

        List<Pxtonzhi> pxtonzhiList =new ArrayList<>();
        for (Pxtonzhi pxtonzhi : Pxlist ){
            Pxtonzhi list=new Pxtonzhi();
            list.setPxId(pxtonzhi.getPxId());
            list.setImportantNum(pxtonzhi.getImportantNum());
            list.setTitle(pxtonzhi.getTitle());
            list.setNgDate_v(DateTimeUtil.dateToStr(pxtonzhi.getNgDate(),"MM-dd"));
            list.setPxHaopingdu(pxtonzhi.getPxHaopingdu());
            pxtonzhiList.add(list);
        }

        PxtonzhiListVo pxtonzhiListVo=new PxtonzhiListVo();
        pxtonzhiListVo.setPxtonzhiList(pxtonzhiList);

        return ServerResponse.createBySuccess(pxtonzhiListVo);
    }

    @Override
    public ServerResponse savePxtonzhi(Pxtonzhi pxtonzhi, Teacher teacher) {

        pxtonzhi.setNgDep(teacher.getTaddress());
        pxtonzhi.setNgDate(new Date());
        pxtonzhi.setNgAuthor(teacher.getTname());
        //1代表起草文件新建的培训通知
        pxtonzhi.setFujian("1");
        pxtonzhi.setPxHaopingdu("0%");

        int status = pxtonzhiMapper.insertSelective(pxtonzhi);

        if (status > 0){
            return ServerResponse.createBySuccess("插入成功！");
        }

        return ServerResponse.createByErrorMessage("插入失败");
    }

    /**
     * 查询培训通知列表
     * @return
     * @param state
     * @param pageNum
     * @param pageSize
     */
    public ServerResponse<PageInfo> getPxtonzhiManagerList(int pageNum, int pageSize, int state) {

        PageHelper.startPage(pageNum,pageSize);

        List<Pxtonzhi> pxtonzhiList = new ArrayList<>();

        if (state == 1) {
            //查询数据库中Fujian等于1和2的数据
            pxtonzhiList = pxtonzhiMapper.selectAllPxtonzhi();
        }
        if (state == 2){
            //查询数据库中Fujian等于0的数据
            pxtonzhiList = pxtonzhiMapper.selectPxtonzhiAsFujianIsThree();
        }

        List<Pxtonzhi> list =new ArrayList<>();
        //取出参训单位分割字符串
        for (Pxtonzhi temp : pxtonzhiList){

            String[] cxstring = temp.getCxDanwei().split(",");

           Pxtonzhi pxtonzhi = new Pxtonzhi();
           pxtonzhi.setPxId(temp.getPxId());
           pxtonzhi.setTitle(temp.getTitle());
           pxtonzhi.setBmEndate_v(DateTimeUtil.dateToStr(temp.getBmEndate(),"yyyy-MM-dd"));
           //只取出为1的状态其他状态为null
           if ("1".equals(temp.getFujian())){
               pxtonzhi.setFujian("1");
           }

           pxtonzhi.setCxCanweiList(Arrays.asList(cxstring));
           pxtonzhi.setNgAuthor(temp.getNgAuthor());
           pxtonzhi.setNgDate_v(DateTimeUtil.dateToStr(temp.getNgDate(), "yyyy-MM-dd"));
           list.add(pxtonzhi);
        }

        PageInfo pageReslut=new PageInfo(pxtonzhiList);
        pageReslut.setList(list);

        return ServerResponse.createBySuccess(pageReslut);
    }

    /**
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ServerResponse pxtonzhiBenchList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        List<Pxtonzhi> pxtonzhiList = pxtonzhiMapper.selectFujianAsOnePxtonzhi();

        List<Pxtonzhi> list =new ArrayList<>();
        for (Pxtonzhi temp : pxtonzhiList){


            Pxtonzhi pxtonzhi = new Pxtonzhi();
            pxtonzhi.setPxId(temp.getPxId());
            pxtonzhi.setTitle(temp.getTitle());
            pxtonzhi.setBmEndate_v(DateTimeUtil.dateToStr(temp.getBmEndate(),"yyyy-MM-dd"));
            pxtonzhi.setFujian(temp.getFujian());

            pxtonzhi.setNgAuthor(temp.getNgAuthor());
            pxtonzhi.setNgDate_v(DateTimeUtil.dateToStr(temp.getNgDate(), "yyyy-MM-dd"));
            list.add(pxtonzhi);
        }

        PageInfo pageReslut=new PageInfo(pxtonzhiList);
        pageReslut.setList(list);

        return ServerResponse.createBySuccess(pageReslut);
    }


    /**
     * 封装培训列表所需的参数
     * @param Pxlist
     * @return
     *//*
    public  List<PxtonzhiListVo> assemblePxListVo(List<Pxtonzhi> Pxlist){

        List<PxtonzhiListVo> pxtonzhiListVoList=new ArrayList<>();
        for (Pxtonzhi pxtongzhi : Pxlist) {
            PxtonzhiListVo pxtonzhiListVo=new PxtonzhiListVo();
            pxtonzhiListVo.setPxId(pxtongzhi.getPxId());
            pxtonzhiListVo.setTitle(pxtongzhi.getTitle());
            pxtonzhiListVo.setNgDate(DateTimeUtil.dateToStr(pxtongzhi.getNgDate(),"MM-dd"));
            pxtonzhiListVo.setImportantNum(pxtongzhi.getImportantNum());
            pxtonzhiListVo.setPxHaopingdu(pxtongzhi.getPxHaopingdu());
            pxtonzhiListVoList.add(pxtonzhiListVo);
        }
        return pxtonzhiListVoList;
    }*/

}

